<?php
/**
 * Handles Comment Post to WordPress and prevents duplicate comment posting.
 *
 */
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
	header('Allow: POST');
	header('HTTP/1.1 405 Method Not Allowed');
	header('Content-Type: text/plain');
	exit;
}

/** Sets up the WordPress Environment. */
//require( '/var/www/met/wp-load.php' );
require(dirname(__FILE__) . '/../../../../wp-load.php');

nocache_headers();

$comment_post_ID = (int) $_POST['comment_post_ID'];

$status = $wpdb->get_row( $wpdb->prepare("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = %d", $comment_post_ID) );

if ( empty($status->comment_status) ) {
	do_action('comment_id_not_found', $comment_post_ID);
	exit;
} elseif ( !comments_open($comment_post_ID) ) {
	do_action('comment_closed', $comment_post_ID);
	wp_die( __('Sorry, comments are closed for this item.') );
} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) {
	do_action('comment_on_draft', $comment_post_ID);
	exit;
} else {
	do_action('pre_comment_on_post', $comment_post_ID);
}

$comment_author       = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
$comment_author_email = ( isset($_POST['email']) )   ? trim($_POST['email']) : null;
$comment_author_url   = ( isset($_POST['url']) )     ? trim($_POST['url']) : null;
$comment_content      = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;

// If the user is logged in
$user = wp_get_current_user();
if ( $user->ID ) {
	if ( empty( $user->display_name ) )
		$user->display_name=$user->user_login;
	$comment_author       = $wpdb->escape($user->display_name);
	$comment_author_email = $wpdb->escape($user->user_email);
	$comment_author_url   = $wpdb->escape($user->user_url);
	if ( current_user_can('unfiltered_html') ) {
		if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) {
			kses_remove_filters(); // start with a clean slate
			kses_init_filters(); // set up the filters
		}
	}
} else {
	if ( get_option('comment_registration') || 'private' == $status->post_status )
		wp_die( __('Sorry, you must be logged in to post a comment.') );
}

$comment_type = '';

if ( get_option('require_name_email') && !$user->ID ) {
	if ( 6 > strlen($comment_author_email) || '' == $comment_author )
		wp_die( __('Error: please fill the required fields (name, email).') );
	elseif ( !is_email($comment_author_email))
		wp_die( __('Error: please enter a valid email address.') );
}

if ( '' == $comment_content )
	wp_die( __('Error: please type a comment.') );

$comment_parent = isset($_POST['comment_parent']) ? absint($_POST['comment_parent']) : 0;

$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'comment_parent', 'user_ID');

$comment_id = yunjie_new_comment( $commentdata );

yunjie_comment_email($comment_id);

$comment = get_comment($comment_id);

if ( !$user->ID ) {
	$comment_cookie_lifetime = apply_filters('comment_cookie_lifetime', 30000000);
	setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
	setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
	setcookie('comment_author_url_' . COOKIEHASH, esc_url($comment->comment_author_url), time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN);
}

// Send Mail
function yunjie_comment_email($commentid) {
	$comment = get_comment($commentid);
/*$fp = @fopen('1.txt','w');
@fwrite($fp, $comment->comment_mail_notify . '\r\n');
@fwrite($fp, $comment->comment_author . '\r\n');
@fclose($fp);*/
	if (empty($comment->comment_parent))
		return;
	$parent = get_comment($comment->comment_parent);
	if (empty($parent->comment_mail_notify) or ($parent->comment_mail_notify == '0'))
		return;
	// 同一人的回复不发送邮件
	if ($comment->comment_author_email == $parent->comment_author_email)
		return;
	$post = get_post($parent->comment_post_ID);
	$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
	$from = "From: \"".'岩之源'."\" <$wp_email>";
	$s = '你在岩之源博客上的评论有新的回复!';
	$m = 'Hi ' . $parent->comment_author . ',<br /><br />';
	$m .= $parent->comment_date . '你在《' . $post->post_title . '》文中说:<br />';
	$m .= $parent->comment_content . '<br />';
	$m .= $comment->comment_author . '于' . $comment->comment_date.'回复:<br />';
	$m .= $comment->comment_content . '<br />';
	$plink = get_permalink($parent->comment_post_ID);
	$clink = $plink . '#comment-'.$parent->comment_ID;
	$m .= '请点此链接查看详细信息: <a href="' . $clink . '" target="_blank">' . $clink . '</a><br />< br/>';
	$m .= '此邮件由岩之源博客(<a target="_blank" href="http://www.mathelite.cn">http://www.mathelite.cn</a>)自动发出，不需回复。<br />';
	$h = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";	
	$m = apply_filters('comment_notification_text', $m, $commentid);
	$s = apply_filters('comment_notification_subject', $s, $commentid);
	$h = apply_filters('comment_notification_headers', $h, $commentid);
//	echo $m;
	@wp_mail($parent->comment_author_email, $s, $m, $h);
	unset($s, $m, $h);
}

//function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
//$location = empty($_POST['redirect_to']) ? get_comment_link($comment_id) : $_POST['redirect_to'] . '#comment-' . $comment_id;
//$location = apply_filters('comment_post_redirect', $location, $comment);

//wp_redirect($location);

function yunjie_new_comment( $commentdata ) {
	$commentdata = apply_filters('preprocess_comment', $commentdata);

	$commentdata['comment_post_ID'] = (int) $commentdata['comment_post_ID'];
	$commentdata['user_ID']         = (int) $commentdata['user_ID'];

	$commentdata['comment_parent'] = absint($commentdata['comment_parent']);
	$parent_status = ( 0 < $commentdata['comment_parent'] ) ? wp_get_comment_status($commentdata['comment_parent']) : '';
	$commentdata['comment_parent'] = ( 'approved' == $parent_status || 'unapproved' == $parent_status ) ? $commentdata['comment_parent'] : 0;

	$commentdata['comment_author_IP'] = preg_replace( '/[^0-9a-fA-F:., ]/', '',$_SERVER['REMOTE_ADDR'] );
	$commentdata['comment_agent']     = $_SERVER['HTTP_USER_AGENT'];

	$commentdata['comment_date']     = current_time('mysql');
	$commentdata['comment_date_gmt'] = current_time('mysql', 1);
	$commentdata['comment_mail_notify'] = (isset($_POST['cmt_mail_notify'])) ? $_POST['cmt_mail_notify'] : 0;

	$commentdata = wp_filter_comment($commentdata);

	$commentdata['comment_approved'] = wp_allow_comment($commentdata);

	$comment_ID = yunjie_insert_comment($commentdata);

	do_action('comment_post', $comment_ID, $commentdata['comment_approved']);

	if ( 'spam' !== $commentdata['comment_approved'] ) { // If it's spam save it silently for later crunching
		if ( '0' == $commentdata['comment_approved'] )
			wp_notify_moderator($comment_ID);

		$post = &get_post($commentdata['comment_post_ID']); // Don't notify if it's your own comment

		if ( get_option('comments_notify') && $commentdata['comment_approved'] && $post->post_author != $commentdata['user_ID'] ) {
			wp_notify_postauthor($comment_ID, $commentdata['comment_type']);
		}
	}

	return $comment_ID;
}

function yunjie_insert_comment($commentdata) {
	global $wpdb;
	extract(stripslashes_deep($commentdata), EXTR_SKIP);

	if ( ! isset($comment_author_IP) )
		$comment_author_IP = '';
	if ( ! isset($comment_date) )
		$comment_date = current_time('mysql');
	if ( ! isset($comment_date_gmt) )
		$comment_date_gmt = get_gmt_from_date($comment_date);
	if ( ! isset($comment_parent) )
		$comment_parent = 0;
	if ( ! isset($comment_approved) )
		$comment_approved = 1;
	if ( ! isset($comment_karma) )
		$comment_karma = 0;
	if ( ! isset($user_id) )
		$user_id = 0;
	if ( ! isset($comment_type) )
		$comment_type = '';
	if ( ! isset($comment_mail_notify) )
		$comment_mail_notify = 0;

	$data = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_date', 'comment_date_gmt', 'comment_content', 'comment_karma', 'comment_approved', 'comment_agent', 'comment_type', 'comment_parent', 'user_id','comment_mail_notify');
	$wpdb->insert($wpdb->comments, $data);

	$id = (int) $wpdb->insert_id;

	if ( $comment_approved == 1 )
		wp_update_comment_count($comment_post_ID);

	$comment = get_comment($id);
	do_action('wp_insert_comment', $id, $comment);

	return $id;
}
?>